﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Linq;
using System.IO;
using MS.InformationManagement.Support;
using System.Web.UI;

namespace MS.InformationManagement.Controls.Layouts.MS.InformationManagement.Controls
{
    public partial class FileViewer : LayoutsPageBase
    {
        #region Properties

        public string CurrentFileName { get { return Request.QueryString.Get("FileName"); } }

        public string ParentControlId { get { return Page.Request["ParentControlId"]; } }

        public int CacheTypeFile
        {
            get
            {
                if (string.IsNullOrEmpty(Page.Request.QueryString["Type"]))
                    return 0;
                else
                    return int.Parse(Page.Request.QueryString["Type"]);
            }
        }

        public List<FileEntity> NonSelectedFiles
        {
            get { return Cache[ParentControlId + SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "Selectedfiles"] as List<FileEntity>; }
        }

        public List<FileEntity> Files
        {
            get
            {
                if (Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "files"] == null)
                    Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "files"] = new List<FileEntity>();
                return Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "files"] as List<FileEntity>;
            }
            set
            {
                Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "files"] = value;
            }
        }

        public List<FileEntity> currentfile
        {
            get
            {
                if (Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "currentfile"] == null)
                    Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "currentfile"] = new List<FileEntity>();
                return Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "currentfile"] as List<FileEntity>;
            }
            set
            {
                Cache[SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain() + "currentfile"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                List<FileEntity> CurrentCacheFiles;

                if (CacheTypeFile == 2)
                    CurrentCacheFiles = currentfile;
                else
                    CurrentCacheFiles = Files;

                var file = (from fl in Files
                            where fl.FileName == CurrentFileName
                            select fl).FirstOrDefault();

                if (file == null)
                    file = (from fl in NonSelectedFiles
                            where fl.FileName == CurrentFileName
                            select fl).FirstOrDefault();

                Response.Clear();
                Response.ClearHeaders();
                Response.AddHeader("Content-Type", "application/octet-stream");
                Response.AddHeader("Content-Length", file.FileContent.Length.ToString());
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlDecode(CurrentFileName));
                Response.BinaryWrite(file.FileContent);
                Response.Flush();
                Response.End();

            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl(ex.Message));
            }
        }
    }
}

////////////////----Presenta problemas con Mozilla
//Response.ContentType = Path.GetExtension(file.FileName);
//Response.AddHeader("content-disposition", "inline;filename=" + Server.UrlDecode(CurrentFileName));
//Response.OutputStream.Write(file.FileContent, 0, file.FileContent.Length);